package cn.tedu._05mvcboot04.exception;

import cn.tedu._05mvcboot04.common.response.JsonResult;
import cn.tedu._05mvcboot04.common.response.StatusCode;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolationException;
import java.util.jar.JarEntry;

@RestControllerAdvice
public class GlobalExceptionHandler {
     @ExceptionHandler
    public JsonResult doHandleMethodArgumentNotValidException(MethodArgumentNotValidException ex){
        String message = ex.getFieldError().getDefaultMessage();
         return new JsonResult(StatusCode.VALIDATED_ERROR,message);
     }

     @ExceptionHandler
    public JsonResult doHandleConstraintViolationException(ConstraintViolationException ex){
         String message =ex.getMessage().split(":")[1].trim();
         return new JsonResult(StatusCode.VALIDATED_ERROR,message);
     }
}
